package com.yylending.las.lending.process.confirmloan;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Constants;
import com.yylending.las.Global;
import com.yylending.las.common.AESEntity;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by fan on 2017/6/16.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class ConfirmLoanModel extends Model {
	private static final String TAG = ConfirmLoanModel.class.getSimpleName();
	public static final int METHOD_CONFIRM_LOAN = 100;
	static final int METHOD_GET_ID_CARD = 101;

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);

		switch (functionalCode) {
			case METHOD_CONFIRM_LOAN:
				return service.confirmLoan(Global.LOGIN_TOKEN, (AESEntity) params);
			case METHOD_GET_ID_CARD:
				return service.getIdCard(Global.LOGIN_TOKEN, (IdCardParams) params);
		}
		return null;
	}

	@Override
	public void onNext(Object entity) {
		CLog.i(TAG, "onNext: " + entity.toString());
		BaseResultEntity result = cast(entity);
		switch (result.requestCode) {
			case METHOD_CONFIRM_LOAN:
				ConfirmLoanResult confirmLoan = cast(entity);
				EventAgent.post(confirmLoan);
				break;
			case METHOD_GET_ID_CARD:
				IdCardResult baseInfo = cast(entity);
				EventAgent.post(baseInfo);
				break;
		}
	}

	private interface Service {
		/**
		 * 借款申请
		 */
		@POST("server/loan/submit")
		Observable<ConfirmLoanResult> confirmLoan(@Header("token") String token, @Body AESEntity params);

		/**
		 * 基本信息查询接口
		 */
		@POST("server/customer/getInfo")
		Observable<IdCardResult> getIdCard(@Header("token") String token, @Body IdCardParams params);

	}

	static class IdCardParams {
		/**
		 * baseInfo: 基本信息; identity: 身份证; sface: 刷新信息; addInfo: 附加信息
		 */
		public String type = "";
	}

	static class IdCardResult extends BaseResultEntity<AesData> {
	}

	static class AesData {
		String json;
	}

	public static class ConfirmLoanParams {
		/**
		 * 借款金额
		 */
		public String businessSum = "";
		/**
		 * 借款期数
		 */
		public String loanTerm = "";
		/**
		 * 经度
		 */
		public String longitude;
		/**
		 * 纬度
		 */
		public String latitude;
		/**
		 * 20: 线上 10: 线下
		 */
		public String isOnline = Constants.LOAN_ONLINE;
	}

	static class ConfirmLoanResult extends BaseResultEntity<ConfirmLoan> {

	}

	public static class ConfirmLoan {
		/**
		 * 返回更新后的额度状态，对应状态300接口，loanState: "200": 借款申请中(直批，等电话状态); "210": 需要现调(线下);
		 */
		String loanState ="";
	}
}